﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WP7Client.UserInterface.Pages;
using System.Collections.Generic;
using Microsoft.Phone.Controls;
using WP7Client.ServerManager;
using WP7Client.Helpers;
using WP7Client.Classes;
using MSH.Common.DAO.Results;
using MSH.Common.Classes;
using GalaSoft.MvvmLight.Command;

namespace WP7Client.Controller
{
    public class SearchResultController : BaseController
    {
        #region Members

        public SearchResultPage SearchResultPage;
        private List<List<Object>> SearchResults = new List<List<Object>>();
        private SearchFilter _Filter;
        public int Skip;

        #endregion

        #region Get&Set


        public ICommand AddFriendCommand
        {
            get;
            internal set;
        }

        public SearchFilter Filter
        {
            get { return _Filter; }
            set { _Filter = value; }
        }

        #endregion       

        public SearchResultController()
        {
            AddFriendCommand = new RelayCommand<String>(SendFriendInvitation);
        }

        /// <summary>
        /// Zoek naar gebruikers aan de hand van hun profiel gegevens
        /// </summary>
        /// <param name="first">Aantal resultaten</param>
        /// <param name="skip">Resultaten vanaf</param>
        /// <param name="filter">Zoek criteria</param>
        public void Search(int first, int skip, SearchFilter filter)
        {
            Loading = true;

            Skip = skip;

            if(filter != null)
                Filter = filter;

            if (Filter == null)
            {
                Loading = false;
                return;
            }
            HttpGet searchHttpGet = new HttpGet(
                String.Format(
                    GlobalHelper.Url +
                    "SearchPeople/{0}?q1={1}&q2={2}&q3={3}&q4={4}&q5={5}&q6={6}&first={7}&skip={8}",
                    UserHelper.Token, Filter.Username, Filter.Firstname, Filter.Surname, Filter.Age, Filter.City, Filter.MobilePhoneNumber, 10, Skip
                ), 
            typeof(SearchPeopleResult), HttpGetMode.Mode1);
            searchHttpGet.FinishedGet += searchHttpGet_FinishedGet;
        }

        private void searchHttpGet_FinishedGet(Object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get == null || get.resultObject == null)
                    return;

                SearchPeopleResult spr = get.resultObject as SearchPeopleResult;
                if (spr != null)
                    ProcessResults(spr);
            }
            catch (Exception ex)
            {

            }
            finally
            {
                Loading = false;
            }
        }

        /// <summary>
        /// Zet het zoekresultaat om in een lijst
        /// </summary>
        /// <param name="spr">Zoek resultaat met daarin een lijst van gebruikers die voldoen aan de criteria</param>
        public void ProcessResults(SearchPeopleResult spr)
        {
            System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                int pivotItemCount = SearchResultPage.PivotSearchResults.Items.Count;
                int k = 0;

                if(!SearchResultPage.PivotItemsBuilt)
                {
                    SearchResultPage.FillPivot((int)spr.ResultCount, 10);
                    pivotItemCount = SearchResultPage.PivotSearchResults.Items.Count;

                    for (int i = 0; i < pivotItemCount; i++)
                    {
                        SearchResults.Add(new List<Object>());
                    }
                    SearchResultPage.PivotItemsBuilt = true;
                }

                for (int i = 0; i < pivotItemCount; i++)
                {
                    if((int)(SearchResultPage.PivotSearchResults.Items[i] as PivotItem).Tag == Skip)
                    {
                        SearchResults[i].Clear();
                        for (int j = 0; j < 10; j++)
                        {
                            if ((spr.ResultCount - Skip) > k)
                            {
                                SearchResults[i].Add(spr.Results[k]);
                                k++;
                            }
                        }
                        PivotItem pi = (SearchResultPage.PivotSearchResults.Items[i] as PivotItem);
                        pi.DataContext = SearchResults[i];
                        ((pi.Content as Grid).Children[0] as ListBox).ItemsSource = SearchResults[i];
                    }
                }

            });
        }

        /// <summary>
        /// Nodig een gevonden gebruiker uit als vriend
        /// </summary>
        /// <param name="username">Gebruikersnaam</param>
        public void SendFriendInvitation(String username)
        {
            ControllerHelper.PeopleNearbyController.SendFriendInvitation(username);
        }

    }
}
